 package CurtainDesign;

 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Container;
 import java.awt.Cursor;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Font;
 import java.awt.Frame;
 import java.awt.GridLayout;
 import java.awt.Point;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import javax.swing.JButton;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JSpinner;
 import javax.swing.JSpinner.NumberEditor;
 import javax.swing.SpinnerNumberModel;
 import javax.swing.SwingUtilities;
 import javax.swing.border.LineBorder;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;

 public class DateChooserJButton extends JButton
 {
   protected String Name = "";
   private DateChooser dateChooser = null;

   private String preLabel = "";

   public DateChooserJButton()
   {
     this(getNowDate());
   }

   public DateChooserJButton(String name) {
     this(getNowDate());
     this.Name = name;
     addChangeListener(new ChangeListener() {
       public void stateChanged(ChangeEvent e) {
         CurtainDesign.m_UserMainGUI.GetOrderInfo().SaveOrderDate(MainView.m_OrderView.m_OrderDate.getDate());

         long deliverDate = CurtainDesign.m_UserMainGUI.GetOrderInfo().GetOrderDate().getTime() + 691200000L;
         CurtainDesign.m_UserMainGUI.GetOrderInfo().SaveDeliverDate(new Date(deliverDate));
       } } );
   }

   public DateChooserJButton(SimpleDateFormat df, String dateString) {
     this();
     setText(df, dateString);
   }

   public DateChooserJButton(Date date)
   {
     this("", date);
   }

   public DateChooserJButton(String preLabel, Date date) {
     if (preLabel != null) {
       this.preLabel = preLabel;
     }
     setDate(date);
     setBorder(null);
     setCursor(new Cursor(12));
//     super.addActionListener(new ActionListener() {
//       public void actionPerformed(ActionEvent e) {
//         if (DateChooserJButton.this.dateChooser == null) {
//           DateChooserJButton.access$002(DateChooserJButton.this, new DateChooser(DateChooserJButton.this, DateChooserJButton.this.Name));
//         }
//         Point p = DateChooserJButton.this.getLocationOnScreen();
//         p.y += 30;
//         DateChooserJButton.this.dateChooser.showDateChooser(p);
//       }
//     });
   }

   private static Date getNowDate() {
     return Calendar.getInstance().getTime();
   }

   private static SimpleDateFormat getDefaultDateFormat() {
     return new SimpleDateFormat("yyyy年MM月dd日");
   }

   public void setText(String s) {
     Date date;
     try {
       date = getDefaultDateFormat().parse(s);
     } catch (ParseException e) {
       date = getNowDate();
     }
     setDate(date);
   }

   public void setText(SimpleDateFormat df, String s) {
     Date date;
     try {
       date = df.parse(s);
     } catch (ParseException e) {
       date = getNowDate();
     }
     setDate(date);
   }

   public void setDate(Date date)
   {
     super.setText(this.preLabel + getDefaultDateFormat().format(date));
   }

   public Date getDate() {
     String dateString = getText().substring(this.preLabel.length());
     try {
       return getDefaultDateFormat().parse(dateString); } catch (ParseException e) {
     }
     return getNowDate();
   }

   public void addActionListener(ActionListener listener)
   {
   }

   private class DateChooser extends JPanel
     implements ActionListener, ChangeListener
   {
     private String Name = "";
     int startYear = 1980;
     int lastYear = 2050;
     int width = 200;
     int height = 200;

     Color backGroundColor = Color.gray;

     Color palletTableColor = Color.white;
     Color todayBackColor = Color.orange;
     Color weekFontColor = Color.blue;
     Color dateFontColor = Color.black;
     Color weekendFontColor = Color.red;

     Color controlLineColor = Color.pink;
     Color controlTextColor = Color.blue;

     Color rbFontColor = Color.white;
     Color rbBorderColor = Color.red;
     Color rbButtonColor = Color.pink;
     Color rbBtFontColor = Color.red;
     JDialog dialog;
     JSpinner yearSpin;
     JSpinner monthSpin;
     JSpinner daySpin;
     JSpinner hourSpin;
     JButton[][] daysButton = new JButton[6][7];

     DateChooser()
     {
       setLayout(new BorderLayout());
       setBorder(new LineBorder(this.backGroundColor, 2));
       setBackground(this.backGroundColor);

       JPanel topYearAndMonth = createYearAndMonthPanal();
       add(topYearAndMonth, "North");
       JPanel centerWeekAndDay = createWeekAndDayPanal();
       add(centerWeekAndDay, "Center");
     }

     DateChooser(String name)
     {
       this.Name = name;
       setLayout(new BorderLayout());
       setBorder(new LineBorder(this.backGroundColor, 2));
       setBackground(this.backGroundColor);

       JPanel topYearAndMonth = createYearAndMonthPanal();
       add(topYearAndMonth, "North");
       JPanel centerWeekAndDay = createWeekAndDayPanal();
       add(centerWeekAndDay, "Center");
     }

     public void restoreYearAndMonthPanel()
     {
       Calendar c = Calendar.getInstance();
       int currentYear = c.get(1);
       int currentMonth = c.get(2) + 1;

       Integer tmp = new Integer(currentYear);
       this.yearSpin.setValue(tmp);
       tmp = new Integer(currentMonth);
       this.monthSpin.setValue(tmp);
     }

     private JPanel createYearAndMonthPanal()
     {
       Calendar c = getCalendar();
       int currentYear = c.get(1);
       int currentMonth = c.get(2) + 1;
       int currentDay = c.get(5);
       int currentHour = c.get(11);
       Font font = new Font("aaa", 0, 12);

       JPanel result = new JPanel();
       result.setLayout(new FlowLayout());
       result.setBackground(this.controlLineColor);

       this.yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, this.startYear, this.lastYear, 1));

       this.yearSpin.setPreferredSize(new Dimension(48, 20));
       this.yearSpin.setName("Year");
       this.yearSpin.setEditor(new NumberEditor(this.yearSpin, "####"));
       this.yearSpin.addChangeListener(this);
       result.add(this.yearSpin);

       JLabel yearLabel = new JLabel("年");
       yearLabel.setForeground(this.controlTextColor);
       yearLabel.setFont(font);
       result.add(yearLabel);

       this.monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1));

       this.monthSpin.setPreferredSize(new Dimension(35, 20));
       this.monthSpin.setName("Month");
       this.monthSpin.addChangeListener(this);
       result.add(this.monthSpin);

       JLabel monthLabel = new JLabel("月");
       monthLabel.setForeground(this.controlTextColor);
       monthLabel.setFont(font);
       result.add(monthLabel);

       return result;
     }

     private JPanel createWeekAndDayPanal()
     {
       String[] colname = { "日", "一", "二", "三", "四", "五", "六" };
       JPanel result = new JPanel();

       result.setFont(new Font("宋体", 0, 12));
       result.setLayout(new GridLayout(7, 7));
       result.setBackground(Color.white);

       for (int i = 0; i < 7; i++) {
         JLabel cell = new JLabel(colname[i]);
         cell.setHorizontalAlignment(4);
         if ((i == 0) || (i == 6))
           cell.setForeground(this.weekendFontColor);
         else {
           cell.setForeground(this.weekFontColor);
         }
         result.add(cell);
       }

       int actionCommandId = 0;
       for (int i = 0; i < 6; i++) {
         for (int j = 0; j < 7; j++) {
           JButton numberButton = new JButton();
           numberButton.setName(this.Name);
           numberButton.setBorder(null);
           numberButton.setHorizontalAlignment(4);
           numberButton.setActionCommand(String.valueOf(actionCommandId));

           numberButton.addActionListener(this);
           numberButton.addMouseListener(new MouseListener() {
             public void mouseClicked(MouseEvent e) {
               int clickCount = e.getClickCount();
               if (clickCount == 2)
                 DateChooser.this.dialog.dispose();
             }

             public void mouseEntered(MouseEvent e)
             {
             }

             public void mouseExited(MouseEvent e)
             {
             }

             public void mousePressed(MouseEvent e)
             {
             }

             public void mouseReleased(MouseEvent e)
             {
             }
           });
           numberButton.setBackground(this.palletTableColor);
           numberButton.setForeground(this.dateFontColor);
           if ((j == 0) || (j == 6))
             numberButton.setForeground(this.weekendFontColor);
           else {
             numberButton.setForeground(this.dateFontColor);
           }
           this.daysButton[i][j] = numberButton;
           result.add(numberButton);
           actionCommandId++;
         }
       }

       return result;
     }

     private JDialog createDialog(Frame owner)
     {
       JDialog result = new JDialog(owner, "日期选择", true);
       result.setDefaultCloseOperation(1);
       result.getContentPane().add(this, "Center");
       result.pack();
       result.setSize(this.width, this.height);
       return result;
     }

     void showDateChooser(Point position) {
       Frame owner = (Frame)SwingUtilities.getWindowAncestor(DateChooserJButton.this);

       if ((this.dialog == null) || (this.dialog.getOwner() != owner)) {
         this.dialog = createDialog(owner);
       }
       this.dialog.setLocation(getAppropriateLocation(owner, position));
       flushWeekAndDay();
       this.dialog.show();
     }

     Point getAppropriateLocation(Frame owner, Point position)
     {
       Point result = new Point(position);
       Point p = owner.getLocation();
       int offsetX = position.x + this.width - (p.x + owner.getWidth());
       int offsetY = position.y + this.height - (p.y + owner.getHeight());

       if (offsetX > 0) {
         result.x -= offsetX;
       }

       if (offsetY > 0) {
         result.y -= offsetY;
       }

       return result;
     }

     private Calendar getCalendar()
     {
       Calendar result = Calendar.getInstance();
       result.setTime(DateChooserJButton.this.getDate());
       return result;
     }

     private int getSelectedYear()
     {
       return ((Integer)this.yearSpin.getValue()).intValue();
     }

     private int getSelectedMonth()
     {
       return ((Integer)this.monthSpin.getValue()).intValue();
     }

     private int getSelectedDate() {
       return ((Integer)this.daySpin.getValue()).intValue();
     }

     private int getSelectedHour() {
       return ((Integer)this.hourSpin.getValue()).intValue();
     }

     private void dayColorUpdate(boolean isOldDay)
     {
       Calendar c = getCalendar();
       int day = c.get(5);
       c.set(5, 1);
       int actionCommandId = day - 2 + c.get(7);
       int i = actionCommandId / 7;
       int j = actionCommandId % 7;
       if (isOldDay)
         this.daysButton[i][j].setForeground(this.dateFontColor);
       else
         this.daysButton[i][j].setForeground(this.todayBackColor);
     }

     private void flushWeekAndDay()
     {
       Calendar c = getCalendar();
       c.set(5, 1);
       int maxDayNo = c.getActualMaximum(5);
       int dayNo = 2 - c.get(7);
       for (int i = 0; i < 6; i++) {
         for (int j = 0; j < 7; j++) {
           String s = "";
           if ((dayNo >= 1) && (dayNo <= maxDayNo)) {
             s = String.valueOf(dayNo);
           }
           this.daysButton[i][j].setText(s);
           dayNo++;
         }
       }
       dayColorUpdate(false);
     }

     public void stateChanged(ChangeEvent e)
     {
       JSpinner source = (JSpinner)e.getSource();
       Calendar c = getCalendar();
       if (source.getName().equals("Hour")) {
         c.set(11, getSelectedHour());
         DateChooserJButton.this.setDate(c.getTime());
         return;
       }

       dayColorUpdate(true);

       if (source.getName().equals("Year"))
         c.set(1, getSelectedYear());
       else if (source.getName().equals("Month"))
       {
         c.set(2, getSelectedMonth() - 1);
       }
       else c.set(5, getSelectedDate());

       DateChooserJButton.this.setDate(c.getTime());
       flushWeekAndDay();
     }

     public void actionPerformed(ActionEvent e)
     {
       JButton source = (JButton)e.getSource();
       if (source.getText().length() == 0) {
         return;
       }

       dayColorUpdate(true);
       source.setForeground(this.todayBackColor);
       int newDay = Integer.parseInt(source.getText());
       Calendar c = getCalendar();
       c.set(5, newDay);
       DateChooserJButton.this.setDate(c.getTime());
     }
   }
 }

/* Location:           C:\Users\Administrator\Desktop\奥特百叶窗\Shutter DesignerV3.1\CurtainDesign.jar
 * Qualified Name:     CurtainDesign.DateChooserJButton
 * JD-Core Version:    0.6.0
 */